
if ( typeof Skt.SlidesSidebar === 'undefined' )
	Skt.SlidesSidebar = {};


Skt.SlidesSidebar.setup = function(sketchpad) {
	Skt.SlidesSidebar.setupSlidesSidebar(sketchpad);
	Skt.SlidesSidebar.bindSlidesSidebarButtons(sketchpad);

	Skt.SlidesSidebar.updateAllCanvasesFromRoomlist(sketchpad.findRoomListByType(Skt.SlideType.PERSONAL));
	Skt.SlidesSidebar.updateAllCanvasesFromRoomlist(sketchpad.findRoomListByType(Skt.SlideType.SCREEN));
};

Skt.SlidesSidebar.setupMiniviewsDragAndDrop = function(roomlist) {
	var id = "#rooms-" + roomlist.getType();
	
	if (roomlist.getType() == Skt.SlideType.GROUP) 
		id = id + "-" + roomlist.getGroup().getId();
	
	$(id).children(".room").draggable({
		helper: "clone",
		appendTo: "body",
		revert: "invalid",
		zIndex: 1000,
		start: function(e, ui) {
			$(ui.helper).addClass("dragged-room");
		},
		stop: function(e, ui) {}
	});
	
	$(id).children(".room").droppable({
		hoverClass: "room-hover",
		accept: ".room",
		drop: function(event, ui) {
			var roomIdCopy = Number(ui.draggable.data("id"));
			var roomIdPaste = Number($(this).data("id"));
			Skt.sketchpad.copyRoom(roomIdCopy);
			Skt.sketchpad.pasteRoom(roomIdPaste, Skt.sketchpad.getUser());
		}
	});
};

Skt.SlidesSidebar.updateRoomlistsBodyHeight = function() {
	$(".slide-type-body").each(function(){
		var height = $(this).height();
		$(this).find(".room-list").css("height", (height - 41) + "px");
	});
};

Skt.SlidesSidebar.updateAllCanvasesFromRoomlist = function(roomlist) {
	roomlist = Skt.RoomList.findSelf(roomlist);
	var editor = roomlist.getSketchpad().getEditor();
	var id = "#rooms-" + roomlist.getType();
	
	if (roomlist.getType() == Skt.SlideType.GROUP) 
		id = id + "-" + roomlist.getGroup().getId();

	$(id).html("");
	
	if ( roomlist.getRooms().length <= 0 ) {
		if ( roomlist.getType() == Skt.SlideType.GROUP )
			$(id).html("<li>This group has no slides</li>");
		else
			$(id).html("<li>There are no " + roomlist.getType() + " slides</li>");
	} else {
		for ( var i = 0; i < roomlist.getRooms().length; ++i ) {
			var room = roomlist.getRooms()[i];
			var cv = editor.getCanvas(room.getId());
			var jMiniview = cv.getJMiniview();
			$(id).append(jMiniview);
		}
	}
	
	Skt.SlidesSidebar.setupMiniviewsDragAndDrop(roomlist);
	Skt.SlidesSidebar.updateRoomlistsBodyHeight();
};

Skt.SlidesSidebar.bindSlidesSidebarButtons = function(sketchpad) {
	$("#add-room-personal").click(function() {
		Skt.Util.postRoomCreation(sketchpad, Skt.SlideType.PERSONAL);
	});
	
	$("#add-room-screen").click(function() {
		Skt.Util.postRoomCreation(sketchpad, Skt.SlideType.SCREEN);
	});
};

Skt.SlidesSidebar.setupSlidesSidebar = function(sketchpad) {
	
	$('#slides-sidebar').tabSlideOut({
	    tabHandle: '#slides-sidebar-handle',  //class of the element that will become your tab
	    pathToTabImage: sketchpad.getBasePath() + '/resources/images/tab-slides.png',    //path to the image for the tab //Optionally can be set using css
	    imageHeight: '152px',                     //height of tab image           //Optionally can be set using css
	    imageWidth: '27px',                       //width of tab image            //Optionally can be set using css
	    tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
	    speed: 300,                               //speed of animation
	    action: 'click',                          //options: 'click' or 'hover', action to trigger animation
	    topPos: '0px',                          //position from the top/ use if tabLocation is left or right
	    leftPos: '0px',                          //position from left/ use if tabLocation is bottom or top
	    minSize: 172,                           //ancho minimo del sidebar en px
	    fixedPosition: true                      //options: true makes it stick(fixed position) on scroll
	});

	$("#slides-sidebar-handle").css("left","-32px");
	
	$("#slides-sidebar-body").each(function() {
		$(this)
			.addClass("ui-accordion ui-accordion-icons ui-widget ui-helper-reset")
			.find("h3")
			.addClass("ui-accordion-header ui-helper-reset ui-state-default")
			.hover(function() { $(this).toggleClass("ui-state-hover"); })
			.prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>')
			.click( function() {
				$(this).toggleClass("ui-accordion-header-active ui-state-active ui-state-default")
					.find("> .ui-icon")
					.toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s")
					.end()
					.next()
						.slideToggle();
				return false;
			})
			.next()
				.addClass("ui-accordion-content ui-helper-reset ui-widget-content")
				.hide();
	});
	
	$(".slide-type-body").resizable({
		handles: { 's' : '.room-list-handle' },
		minHeight: 70,
		maxHeight: 450,
		resize: function(event, ui) {
			var height = $(this).height();
			$(this).css("height", height + "px");
			Skt.SlidesSidebar.updateRoomlistsBodyHeight();
		},
		stop: function(event, ui) {
			$(this).css("width","100%");
		}
	});
	
	$("#groupal-slides-body").resizable({
		handles: { 's': '.room-list-handle' },
		minHeight: 70,
		maxHeight: 450,
		resize: function(event, ui) {
			var height = $(this).height();
			$(this).children("#groups-list").css("height", height + "px");
		},
		stop: function(event, ui) {
			$(this).css("width", "100%");
		}
	});
	
};


